#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Licensed under the GNU General Public License, version 3.
# See the file http://www.gnu.org/copyleft/gpl.txt

import sys
sys.path.insert(0,"/usr/lib/sulin")

from inary.actionsapi import get
from inary.actionsapi import autotools
from inary.actionsapi import inarytools
from inary.actionsapi import shelltools

shelltools.cd("../Python-{}".format(get.srcVERSION()))

def setup():
    inarytools.flags.add("-fwrapv")

    inarytools.dosed("Lib/cgi.py","^#.* /usr/local/bin/python","#!/usr/bin/python3")
    options = """--prefix=/usr \
              --enable-optimizations \
              --with-lto --prefix=/usr \
              --enable-shared \
              --with-computed-gotos \
              --enable-ipv6 \
              --with-dbmliborder=gdbm:ndbm \
              --without-ensurepip \
              --enable-loadable-sqlite-extensions """
    shelltools.export("CFLAGS","-O3 -s -fno-semantic-interposition")
    if get.buildTYPE() == "emul32":
         shelltools.export("CC","gcc -m32")
         shelltools.export("CXX","g++ -m32")
         options += "--libdir=/usr/lib32"
    else:
         options += "--libdir=/usr/lib"

    autotools.rawConfigure(options)

def build():
    autotools.make()

#def check():
#    autotools.make('test')

def install():
    autotools.rawInstall("DESTDIR=%s" % get.installDIR())
    if get.buildTYPE()=="emul32":
        return
    inarytools.removeDir("/usr/lib/python3.9/test")
    inarytools.dosym("/usr/bin/python3","/etc/alternative/python/python3.9")
    shelltools.system("find {} -type d | grep __pycache__ | xargs rm -rvf".format(get.installDIR()))
    inarytools.dodoc("LICENSE", "README*")
